package com.snailcatmall.www.serversocket.tools;

import com.snailcatmall.www.serversocket.pojo.ClientInfo;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

import java.net.SocketAddress;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class GlobalStaticClass {
    public static Map<SocketAddress, ClientInfo> channelHandlerContextMap = new HashMap<>();
    public static ThreadPoolExecutor threadPoolExecutor =new ThreadPoolExecutor(3,20,0, TimeUnit.SECONDS,new LinkedBlockingQueue(100), Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());
    public static EventLoopGroup bossGroup = new NioEventLoopGroup(); //bossGroup就是parentGroup，是负责处理TCP/IP连接的
    public static EventLoopGroup workerGroup = new NioEventLoopGroup(); //workerGroup就是childGroup,是负责处理Channel(通道)的I/O事件
    public static ServerBootstrap serverBootstrap = new ServerBootstrap();

    /**
     * @param ch 客户端连接后的套接字
     */
    public static void AddClientMap(ChannelHandlerContext ch) {
        ClientInfo clientInfo = new ClientInfo();
        clientInfo.setIsDbClient(0);
        List<String> stringList = new ArrayList<>();
        stringList.add("ALL");
        clientInfo.setGroup(stringList);
        clientInfo.setChannelHandlerContext(ch);
        clientInfo.setTimeStamp(Calendar.getInstance().getTimeInMillis());
        GlobalStaticClass.channelHandlerContextMap.put(ch.channel().localAddress(), clientInfo);
        System.out.println("初始化默认组: " +clientInfo.toString());
        System.out.println("当前在线人数: " + GlobalStaticClass.channelHandlerContextMap.size() +" 位");
    }
}
